/**
 * 
 */
package com.wutianyi.jta;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;

import javax.sql.DataSource;
import javax.sql.XAConnection;
import javax.sql.XADataSource;
import javax.transaction.UserTransaction;
import javax.transaction.xa.XAException;
import javax.transaction.xa.XAResource;
import javax.transaction.xa.Xid;

import org.junit.Test;

import com.google.appengine.api.search.ExpressionParser.expression_return;
import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;
import com.mysql.jdbc.jdbc2.optional.MysqlXADataSource;

/**
 * @author hanjiewu
 * 
 *         2013-7-12-上午11:54:43
 */
public class JtaMain
{
    public static XADataSource getDataSource()
    {
        MysqlXADataSource mysqlDataSource = new MysqlXADataSource();
        mysqlDataSource.setUrl("jdbc:mysql://localhost:3307/test");
        mysqlDataSource.setUser("root");
        mysqlDataSource.setPassword("860728");
        return mysqlDataSource;
    }

    @Test
    public void example1() throws SQLException
    {
        XADataSource xaDS;
        XAConnection xaCon;
        XAResource xaRes;
        Xid xid;
        Connection con;
        Statement stmt;
        int ret;
        xaDS = getDataSource();
        xaCon = xaDS.getXAConnection();
        xaRes = xaCon.getXAResource();
        con = xaCon.getConnection();
        stmt = con.createStatement();
        xid = new MyXid(100, new byte[]
        { 0x01 }, new byte[]
        { 0x02 });
        try
        {
            xaRes.start(xid, XAResource.TMNOFLAGS);
            stmt.executeUpdate("insert into test_table values (100)");
            xaRes.end(xid, XAResource.TMSUCCESS);
            ret = xaRes.prepare(xid);
            if (ret == XAResource.XA_OK)
            {
                xaRes.commit(xid, false);
            }
        }
        catch (XAException e)
        {
            e.printStackTrace();
        }
        finally
        {
            stmt.close();
            con.close();
            xaCon.close();
        }
    }

    @Test
    public void example2() throws SQLException
    {
        XADataSource xaDS;
        XAConnection xaCon;
        XAResource xaRes;
        Xid xid;
        Connection con;
        Statement stmt;
        int ret;

        xaDS = getDataSource();
        xaCon = xaDS.getXAConnection();
        xaRes = xaCon.getXAResource();
        con = xaCon.getConnection();
        stmt = con.createStatement();
        xid = new MyXid(0, new byte[]
        { 0x01 }, new byte[]
        { 0x02 });
        try
        {
            xaRes.start(xid, XAResource.TMNOFLAGS);
            stmt.execute("insert into test_table values(200)");
            xaRes.end(xid, XAResource.TMSUCCESS);
            ret = xaRes.prepare(xid);
            if (ret == XAResource.XA_OK)
            {
                xaRes.rollback(xid);
            }
        }
        catch (XAException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        finally
        {
            stmt.close();
            con.close();
            xaCon.close();
        }

    }

    @Test
    public void example3() throws SQLException
    {
        XADataSource xaDS;
        XAConnection xaCon;
        XAResource xaRes;
        Xid xid;
        Connection con;
        Statement stmt;

        xaDS = getDataSource();
        xaCon = xaDS.getXAConnection();
        xaRes = xaCon.getXAResource();
        con = xaCon.getConnection();
        stmt = con.createStatement();

        xid = new MyXid(0, new byte[]
        { 0x01 }, new byte[]
        { 0x02 });

        try
        {
            xaRes.start(xid, XAResource.TMNOFLAGS);
            stmt.executeUpdate("insert into test_table values(301)");
            xaRes.end(xid, XAResource.TMSUSPEND);
            stmt.executeUpdate("insert into test_table values(302)");

            xaRes.start(xid, XAResource.TMRESUME);
            stmt.executeUpdate("insert into test_table values(303)");
            xaRes.start(xid, XAResource.TMSUCCESS);

            int ret = xaRes.prepare(xid);
            if (ret == XAResource.XA_OK)
            {
                xaRes.rollback(xid);
            }
        }
        catch (XAException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        finally
        {
            stmt.close();
            con.close();
            xaCon.close();
        }
    }

    @Test
    public void example4()
    {
        XADataSource xaDS;
        XAConnection xaCon;
        XAResource xaRes;
        Xid xid1;
        Xid xid2;
        Connection con;
        Statement stmt;
        
        XADataSource xaDS2;
        XAConnection xaCon2;
        XAResource xaRes2;
        Connection con2;
        Statement stmt2;

        xid1 = new MyXid(0, new byte[]
        { 0x01 }, new byte[]
        { 0x02 });
        xid2 = new MyXid(0, new byte[]
        { 0x21 }, new byte[]
        { 0x22 });

        xaDS = getDataSource();
        xaDS2 = getDataSource();
        try
        {
            xaCon = xaDS.getXAConnection();
            xaRes = xaCon.getXAResource();
            con = xaCon.getConnection();
            stmt = con.createStatement();
            
            xaCon2 = xaDS2.getXAConnection();
            xaRes2 = xaCon2.getXAResource();
            con2 = xaCon2.getConnection();
            stmt2 = con2.createStatement();

            xaRes.start(xid1, XAResource.TMNOFLAGS);
            stmt.executeUpdate("insert into test_table values(401)");
            xaRes.end(xid1, XAResource.TMSUCCESS);
            int ret = xaRes.prepare(xid1);
            
            xaRes2.start(xid2, XAResource.TMJOIN);

            stmt2.executeUpdate("insert into test_table values(402)");
            xaRes2.end(xid2, XAResource.TMSUCCESS);
            
            if (ret == XAResource.XA_OK)
            {
                xaRes.commit(xid1, false);
            }
            
            ret = xaRes.prepare(xid2);
            if (ret == XAResource.XA_OK)
            {
                xaRes2.rollback(xid2);
            }
        }
        catch (SQLException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        catch (XAException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    public static void main(String[] args) throws SQLException
    {
    }
}
